using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using SukiUI.Dialogs;

namespace AvaloniaApplication1.Models;

public enum ConnectionType
{
    ModbusRtu,
    ModbusRtuOverTcp,
}
/// <summary>
/// 设备卡片的的模型
/// </summary>
public class DeviceCardModel: INotifyPropertyChanged
{
    /// <summary>
    /// 设备名称
    /// </summary>
    private string _deviceName;
    /// <summary>
    /// 连接状态
    /// </summary>
    private bool _isConnected;

    private Dictionary<string,object> _modbusParams;


    /// <summary>
    /// 连接方式
    /// </summary>
    private ConnectionType _connectionType;
    public string DeviceName
    {
        get => _deviceName;
        set=> SetField(ref _deviceName, value);
    }
    public bool IsConnected
    {
        get => _isConnected;
        set=> SetField(ref _isConnected, value);
    }
    public ConnectionType ConnectionType
    {
        get => _connectionType;
        set=> SetField(ref _connectionType, value);
    }
    public Dictionary<string,object> ModbusParams
    {
        get => _modbusParams;
        set=> SetField(ref _modbusParams, value);
    }


    public event PropertyChangedEventHandler? PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    public DeviceCardModel(string deviceName,bool isConnected,ConnectionType connectionType,Dictionary<string,object> modbusParams)
    {
        DeviceName = deviceName;
        IsConnected=isConnected;
        ConnectionType=connectionType;
        ModbusParams=modbusParams;
    }

}